# Maintainer: Sebastian Ehlert <awvwgk@disroot.org>

_realname=fpm
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.9.0
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64')
pkgdesc="Fortran package manager (mingw-w64)"
url="https://github.com/fortran-lang/fpm"
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-omp"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"))
makedepends=("${MINGW_PACKAGE_PREFIX}-fc"
             "git")
optdepends=("git: Support for fetching projects with git")
source=(${_realname}-${pkgver}.zip::"${url}/releases/download/v${pkgver}/fpm-${pkgver}.zip"
        ${_realname}-${pkgver}.F90::"${url}/releases/download/v${pkgver}/fpm-${pkgver}.F90")
sha256sums=('484debabd7d22186ac41f865ddf63475c279a61a51aaff5636ed615860b5b8d7'
            '556b3883ba1db6fd62c99ba7143f1e97ebb409729c4209f03aa3c6fc970cce3f')
noextract=(${_realname}-${pkgver}.F90)

build() {
  cd "${srcdir}/${_realname}-${pkgver}"
  local _build="build-${MSYSTEM}"
  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    _fc="${MINGW_PREFIX}/bin/gfortran"
    _fflags="-g -O3 -fbacktrace -fopenmp"
  else
    _fc="${MINGW_PREFIX}/bin/flang"
    _fflags="-g -O3 -fopenmp"
  fi

  mkdir -p "${_build}/bootstrap"
  "${_fc}" -J "${_build}/bootstrap" -o "${_build}/bootstrap/fpm" ${srcdir}/${_realname}-${pkgver}.F90

  "${_build}/bootstrap/fpm" install \
    --compiler "${_fc}" \
    --flag "${_fflags}" \
    --prefix "${_build}"
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"
  install -Dm755 build-${MSYSTEM}/bin/fpm "${pkgdir}"${MINGW_PREFIX}/bin/fpm
  install -Dm644 LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
